
//生成过滤器城市选项
var records = data.DATA.RECORD, citys = [], tmp = [],
	cityOptionTpl = '<input type="checkbox" name="filter-city" id="filter-city-{$i}" value="{$n}"><label for="filter-city-{$i}">{$n}</label>';
//过滤重复城市
for(var i=0; i<records.length; i++) {
	var area = records[i].AREA;
	if(citys.join().indexOf(area)<0) citys.push(area);
}
for(var i=0; i<citys.length; i++) {
	tmp.push(cityOptionTpl.replace('{$i}', i).replace('{$i}', i).replace('{$n}', citys[i]).replace('{$n}', citys[i]));
}
$('#filter-citys').html(tmp.join('\n'));
tmp = [];

//console.log(records);
//更新列表
function updateContents(records) {
	var shopTpl = '<div class="content {$stat}bar">\
		<div class="main">\
			<p class="shop-name">{$name}</p>\
			<p class="shop-info">{$info}</p>\
		</div>\
		<div class="addi">\
			<p class="shop-addr">{$addr}</p>\
			<p class="shop-upda">{$upda}</p>\
		</div>\
	</div>', tmp = [];
	for(var i=0; i<records.length; i++) {
		var rec = records[i];
		tmp.push( shopTpl.replace('{$name}', rec.NAME)
						.replace('{$stat}', rec.stat_word)
						.replace('{$info}', rec.MACHINE_COUNT+'台机、'+rec.TOTAL_PLAYERS*1+'人、'+rec.TOTAL_PLAYCOUNT*1+'次、'+Math.floor(rec.TOTAL_PLAYTIME/60)+'分钟')
						.replace('{$addr}', rec.LOCATION)
						.replace('{$upda}', rec.strhm+'更新') );
	}
	$('#contents').html(tmp.join('\n'));
	$('#contents .addi').hide();
}

updateContents(records);

//收回/展开项目
$('.content').live('click', function(){
	var a = $('.addi', this);
	a.slideToggle();
});

//切换过滤器
var $filter = {
	status: $('#filter .status'),
	city: $('#filter .city')
};
$('#is-filter').change(function(){
	if(this.checked) {
		$filter.city.show();
		$filter.status.show();
	} else {
		$filter.city.hide();
		$filter.status.hide();
		updateContents(records);
	}
});

//过滤
var $filterStatusCheckbox = $('#filter input[name=filter-status]'),
	$filterCityCheckbox = $('#filter input[name=filter-city]');
$filterStatusCheckbox.change(function(){
	var tmp = [], s, r = [];
	for(var i=0; i<$filterStatusCheckbox.length; i++) {
		if($filterStatusCheckbox[i].checked) tmp.push($filterStatusCheckbox[i].value);
	}
	if(tmp.length===0) {
		updateContents(records);
		return ;
	}
	s = tmp.join();
	for(var i=0; i<records.length; i++) {
		if(s.indexOf(records[i].chn_word)>=0) r.push(records[i]);
	}
	updateContents(r);
});
$filterCityCheckbox.change(function(){
	var tmp = [], s, r = [];
	for(var i=0; i<$filterCityCheckbox.length; i++) {
		if($filterCityCheckbox[i].checked) tmp.push($filterCityCheckbox[i].value);
	}
	
	if(tmp.length===0) {
		updateContents(records);
		return ;
	}
	s = tmp.join();
	for(var i=0; i<records.length; i++) {
		if(s.indexOf(records[i].AREA)>=0) r.push(records[i]);
	}
	updateContents(r);
});




